<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--片区数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="areaName" placeholder="请输入名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-containers">
          <el-tree :data="areaOptions" :props="defaultProps" :expand-on-click-node="false"
            :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
            @node-click="handleNodeClick" />
        </div>
      </el-col>

      <!--小区数据-->
      <el-col :span="20" :xs="24">
        <el-card class="box-card">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="客户类型" prop="customerType">
              <el-select v-model="queryParams.customerType" filterable placeholder="请选择">
                <el-option v-for="dict in dict.type.customer_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>

            </el-form-item>
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="身份证号" prop="idNumber">
              <el-input v-model="queryParams.idNumber" placeholder="请输入身份证号" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="客户编码" prop="customerCode">
              <el-input v-model="queryParams.customerCode" placeholder="请输入客户编码" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="开户时间" prop="openDate">
              <el-date-picker type="date" placeholder="选择开户时间" v-model="queryParams.openDate" style="width: 150px"
                value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>

            <el-form-item label="手机号" prop="telephone">
              <el-input v-model="queryParams.telephone" placeholder="请输入手机号" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item label="计价方案" prop="priceSchemeId">
              <el-select v-model="queryParams.priceSchemeId" filterable placeholder="请选择">
                <el-option :label="item.schemeName" :value="item.id" v-for="(item, index) in schemeList" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="小区名称" prop="areaName">
              <el-input v-model="queryParams.areaName" placeholder="请输入小区名称" clearable style="width: 150px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['system:customerarchives:add']">新增</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                v-hasPermi="['system:customerarchives:import']">导入</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImportOld"
                v-hasPermi="['system:customerarchives:importold']">旧数据导入</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['system:customerarchives:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="userList" border @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="客户名称" align="center" key="customerName" prop="customerName"
              :show-overflow-tooltip="true" />
            <el-table-column label="客户编码" align="center" key="customerCode" prop="customerCode"
              :show-overflow-tooltip="true" />
            <el-table-column prop="status" label="客户类型" width="80">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.customer_type" :value="scope.row.customerType" />
              </template>
            </el-table-column>
            <el-table-column label="计价方案" align="center" key="priceSchemeName" prop="priceSchemeName"
                             :show-overflow-tooltip="true" />
            <el-table-column label="小区" align="center" key="areaName" prop="areaName" :show-overflow-tooltip="true"
            width="100" />
            <el-table-column label="证件号" align="center" key="idNumber" prop="idNumber" :show-overflow-tooltip="true"
              width="100" />
            <el-table-column label="总购气金额" align="center" key="totalAmout" prop="totalAmout" :show-overflow-tooltip="true"
              width="120" />
            <el-table-column label="余额" align="center" key="remainAmout" prop="remainAmout" :show-overflow-tooltip="true"
              width="100" />
            <!--            <el-table-column label="总用气量" align="center" key="totalVolume" prop="totalVolume"-->
            <!--              :show-overflow-tooltip="true" width="120" />-->
            <!--            <el-table-column label="剩余气量" align="center" key="remainVolume" prop="remainVolume"-->
            <!--              :show-overflow-tooltip="true" width="120" />-->
            <el-table-column label="计费周期开始时间" align="center" key="cycleStartTime" prop="cycleStartTime"
              :show-overflow-tooltip="true" width="150" />
            <el-table-column label="计费周期结束时间" align="center" key="cycleEndTime" prop="cycleEndTime"
              :show-overflow-tooltip="true" width="150" />
            <el-table-column label="周期内总用气量" align="center" key="cycleUseGas" prop="cycleUseGas"
              :show-overflow-tooltip="true" width="150" />
            <el-table-column label="周期内总使用金额" align="center" key="cycleUseAmount" prop="cycleUseAmount"
              :show-overflow-tooltip="true" width="150" />
            <el-table-column label="充值次数" align="center" key="payCount" prop="payCount" :show-overflow-tooltip="true" />
            <el-table-column label="单元号" align="center" key="unit" prop="unit" :show-overflow-tooltip="true"
              width="100" />
            <el-table-column label="楼栋号" align="center" key="building" prop="building" :show-overflow-tooltip="true" />
            <el-table-column label="楼层号" align="center" key="floor" prop="floor" :show-overflow-tooltip="true" />
            <el-table-column label="门牌号" align="center" key="houseNo" prop="houseNo" :show-overflow-tooltip="true" />
            <el-table-column label="客户地址" align="center" key="customerAddress" prop="customerAddress"
              :show-overflow-tooltip="true" />
            <el-table-column label="联系电话" align="center" key="telephone" prop="telephone" :show-overflow-tooltip="true"
              width="120" />
            <el-table-column label="账单日" align="center" key="billDay" prop="billDay" :show-overflow-tooltip="true"
              width="120" />
            <el-table-column label="开户日期" align="center" key="openDate" prop="openDate" :show-overflow-tooltip="true" />
            <el-table-column label="自动发送短信" align="center" key="isAutoSendmsg" prop="isAutoSendmsg"
              :show-overflow-tooltip="true" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.isAutoSendmsg == 'N'">否</span>
                <span v-if="scope.row.isAutoSendmsg == 'Y'">是</span>
              </template>
            </el-table-column>

            <el-table-column label="是否欠费关阀" align="center" key="isCloseOverdue" prop="isCloseOverdue"
              :show-overflow-tooltip="true" width="120">
              <template slot-scope="scope">
                <span v-if="scope.row.isCloseOverdue == 'N'">否</span>
                <span v-if="scope.row.isCloseOverdue == 'Y'">是</span>
              </template>
            </el-table-column>
            <el-table-column label="是否缴费开阀" align="center" key="isOpenPaid" prop="isOpenPaid"
              :show-overflow-tooltip="true" width="100">
              <template slot-scope="scope">
                <span v-if="scope.row.isOpenPaid == 'N'">否</span>
                <span v-if="scope.row.isOpenPaid == 'Y'">是</span>
              </template>
            </el-table-column>
            <el-table-column label="短信提醒号码" align="center" key="messagePhone" prop="messagePhone"
              :show-overflow-tooltip="true" width="120" />

            <!-- <el-table-column label="充值次数" align="center" key="payCount" prop="payCount" :show-overflow-tooltip="true" /> -->

            <el-table-column label="备注" align="center" key="remark" prop="remark" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:customerarchives:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                  v-hasPermi="['system:customerarchives:remove']">删除</el-button>
                <el-button size="mini" type="text" icon="el-icon-tickets" @click="handlAaddBj(scope.row)"
                  v-hasPermi="['system:meterarchives:add']" v-if="!scope.row.meterId">新增表计</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-card>

      </el-col>
    </el-row>

    <!-- 添加或修改小区配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-collapse v-model="activeNames" accordion>
        <el-collapse-item title="基本信息" name="1">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>

              <el-col :span="8" v-if="title == '添加客户档案'">
                <el-form-item label="客户编号" prop="customerCode">
                  <el-input v-model="form.customerCode" placeholder="请输入客户编号" disabled style="width:80%" />
                  <i class="el-icon-refresh" @click="refreshNumber" style="cursor: pointer;margin-left: 5px;"></i>
                </el-form-item>
              </el-col>
              <el-col :span="8" v-if="title == '修改客户档案'">
                <el-form-item label="客户编号" prop="customerCode">
                  <el-input v-model="form.customerCode" placeholder="请输入客户编号" disabled />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户名称" prop="customerName" required>
                  <el-input v-model="form.customerName" placeholder="请输入客户名称" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户类型" prop="customerType">
                  <el-select v-model="form.customerType" filterable placeholder="请选择">
                    <el-option v-for="dict in dict.type.customer_type" :key="dict.value" :label="dict.label"
                      :value="dict.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="所属小区" prop="areaId" required>
                  <treeselect v-model="form.areaId" :options="areaOptions" :show-count="true" placeholder="请选择所属小区" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="证件号" prop="idNumber" required>
                  <el-input v-model="form.idNumber" placeholder="请输入证件号" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="开户日期" prop="openDate">
                  <el-date-picker v-model="form.openDate" type="date" placeholder="选择日期"
                    value-format="yyyy-MM-dd HH:mm:ss" style="width:100%">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话" prop="telephone" required>
                  <el-input v-model="form.telephone" placeholder="请输入联系电话" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="用气类型" prop="gasType">
                  <el-select v-model="form.gasType" filterable placeholder="请选择">
                    <el-option v-for="dict in dict.type.gas_type" :key="dict.value" :label="dict.label"
                      :value="dict.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="楼栋号" prop="building">
                  <el-input v-model="form.building" placeholder="请输入楼栋号" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单元号" prop="unit">
                  <el-input v-model="form.unit" placeholder="请输入单元号" maxlength="30" />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="楼层号" prop="floor">
                  <el-input v-model="form.floor" placeholder="请输入楼层号" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="门牌号" prop="houseNo">
                  <el-input v-model="form.houseNo" placeholder="请输入门牌号" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="客户地址" prop="customerAddress">
                  <el-input v-model="form.customerAddress" placeholder="请输入客户地址" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" placeholder="请输入备注" maxlength="30" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-collapse-item>
        <el-collapse-item title="计价信息" name="2">
          <el-form ref="form" :model="form" label-width="120px">
            <el-row>

              <el-col :span="8">
                <el-form-item label="自动发送短信" prop="isAutoSendmsg">
                  <el-select v-model="form.isAutoSendmsg" filterable placeholder="请选择">
                    <el-option label="是" value="Y"> </el-option>
                    <el-option label="否" value="N"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="是否欠费关阀" prop="isCloseOverdue">
                  <el-select v-model="form.isCloseOverdue" filterable placeholder="请选择">
                    <el-option label="是" value="Y"> </el-option>
                    <el-option label="否" value="N"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="是否缴费开阀" prop="isOpenPaid">
                  <el-select v-model="form.isOpenPaid" filterable placeholder="请选择">
                    <el-option label="是" value="Y"> </el-option>
                    <el-option label="否" value="N"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="短信提醒号码" prop="messagePhone">
                  <el-input v-model="form.messagePhone" placeholder="请输入短信提醒号码" maxlength="30" />
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="账单日" prop="billDay">
                  <el-input v-model="form.billDay" placeholder="请输入账单日" maxlength="30" />
                </el-form-item>
              </el-col>

            </el-row>
          </el-form>
        </el-collapse-item>
      </el-collapse>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 新增表计弹框 -->
    <el-dialog :title="title2" :visible.sync="bjOPen" width="1000px" append-to-body>

      <el-form ref="form1" :model="form1" :rules="rules1" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="form1.customerName" placeholder="请输入客户名称" maxlength="30" disabled />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="表号" prop="meterCode">
              <el-input v-model="form1.meterCode" placeholder="请输入表号" maxlength="30" />
              <!-- <el-input v-model="form1.meterCode" placeholder="请输入表号" maxlength="30" disabled style="width:80%"/>
              <i class="el-icon-refresh" @click="refreshNumber1" style="cursor: pointer;margin-left: 5px;"></i> -->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商" prop="meterManufacturer">
              <el-select v-model="form1.meterManufacturer" filterable placeholder="请选择" @change="getMeterListBySupplier">
                <el-option :label="item.supplierName" :value="item.id" v-for="(item, index) in supplierList" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="表计类型" prop="meterType">
              <el-select v-model="form1.meterType" filterable placeholder="请选择">
                <el-option :label="item.typeName" :value="item.id" v-for="(item, index) in meterBySupplierList"
                  :key="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计价方案" prop="priceSchemeId">
              <el-select v-model="form1.priceSchemeId" filterable placeholder="请选择">
                <el-option :label="item.schemeName" :value="item.id" v-for="(item, index) in schemeList" :key="index">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="表计状态" prop="meterStatus">
              <el-select v-model="form1.meterStatus" filterable placeholder="请选择">
                <el-option label="待安装" :value="1"> </el-option>
                <el-option label="正常" :value="2"> </el-option>
                <el-option label="已拆卸" :value="3"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="DTU/RTU编号" prop="dtuCode">
              <el-input v-model="form1.dtuCode" placeholder="不能输入表号" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="IMEI" prop="imei">
              <el-input v-model="form1.imei" placeholder="请输入IMEI" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="基表对应ICCID" prop="iccid">
              <el-input v-model="form1.iccid" placeholder="请输入基表对应ICCID" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="进气方向" prop="intakeDirection">
              <el-radio-group v-model="form1.intakeDirection">
                <el-radio label="L">左进</el-radio>
                <el-radio label="R">右进</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="是否带GPRS" prop="isGprs">
              <el-select v-model="form1.isGprs" filterable placeholder="请选择">
                <el-option label="否" value="N"> </el-option>
                <el-option label="是" value="Y"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="安装日期" prop="installTime">
              <el-date-picker v-model="form1.installTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期" style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报警额度" prop="alarmGas">
              <el-input v-model="form.alarmGas" placeholder="请输入报警额度" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="基表底数" prop="baseNumber">
              <el-input v-model="form.baseNumber" placeholder="请输入基表底数" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="通讯地址" prop="mailAddress">
              <el-input v-model="form.mailAddress" placeholder="请输入通讯地址" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="通讯协议" prop="mailProtocol">
              <el-input v-model="form.mailProtocol" placeholder="请输入通讯协议" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否带流量计" prop="isFlowmeter">
              <el-select v-model="form1.isFlowmeter" filterable placeholder="请选择">
                <el-option label="否" value="N"> </el-option>
                <el-option label="是" value="Y"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="流量计厂家" prop="flowmeterManufacturer">
              <el-input v-model="form1.flowmeterManufacturer" placeholder="请输入流量计厂家" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="流量计起始量" prop="initialGas">
              <el-input v-model="form1.initialGas" placeholder="请输入流量计起始量" maxlength="30" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--          <div class="el-upload__tip" slot="tip">-->
          <!--            <el-checkbox v-model="upload.updateSupport" />-->
          <!--            是否更新已经存在的客户数据-->
          <!--          </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 旧客户数据导入对话框 -->
    <el-dialog :title="uploadold.title" :visible.sync="uploadold.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="uploadold.headers"
        :action="uploadold.url + '?updateSupport=' + uploadold.updateSupport" :disabled="uploadold.isUploading"
        :on-progress="handleFileUploadProgressOld" :on-success="handleFileSuccessOld" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--          <div class="el-upload__tip" slot="tip">-->
          <!--            <el-checkbox v-model="upload.updateSupport" />-->
          <!--            是否更新已经存在的客户数据-->
          <!--          </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
            @click="importTemplateOld">下载旧客户数据模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileFormOld">确 定</el-button>
        <el-button @click="uploadold.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { areaList, areaTreeList } from "@/api/fileManagement/areainfo.js"
import { customeList, customeInfo, addCustome, updateArea, delCustome, exportCustome, generateNumber } from "@/api/fileManagement/civilArchives.js"
import { getGasmeterList, listGasmeter } from '@/api/systemSetup/gasMeter.js'
import { getToken } from "@/utils/auth";
import { getSupplierList, } from "@/api/system/supplierInfo.js";
import { addMeterarchives, } from "@/api/fileManagement/meterarchives.js"
import { planSelectList, } from '@/api/service/planlist.js'
import Treeselect from '@riophae/vue-treeselect'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "CivilArchives",
  components: { Treeselect },
  dicts: ["gas_type", "customer_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      idName: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区表格数据
      userList: null,
      // 弹出层标题
      title: "",
      //表计弹出层标题
      title2: "",
      // 片区树选项
      areaOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 片区名称
      areaName: '',
      //是否表计显示弹出层
      bjOPen: false,
      // 表单参数
      form: {},
      form1: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 客户导入参数
      upload: {
        // 是否显示弹出层（客户导入）
        open: false,
        // 弹出层标题（客户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的客户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/customerarchives/importData",
      },
      // 旧客户数据导入参数
      uploadold: {
        // 是否显示弹出层（客户导入）
        open: false,
        // 弹出层标题（客户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的客户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/customerarchives/importDataOld",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerType: null,
        customerName: null,
        idNumber: null,
        deptId: null,
        openDate: null,
        telephone: null,
        customerCode: null,
        areaName: null
      },
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
        ],
        idNumber: [
          { required: true, message: "证件号不能为空", trigger: "blur" },
        ],
        telephone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
        customerType: [
          { required: true, message: "客户类型不能为空", trigger: "change" },
        ],
        areaId: [
          { required: true, message: "所属小区不能为空", trigger: "change" },
        ],
      },
      rules1: {
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
        ],
        meterCode: [
          { required: true, message: "表号不能为空", trigger: "blur" },
        ],
        meterManufacturer: [
          { required: true, message: "供应商不能为空", trigger: "change" },
        ],
        // meterModel: [
        //   { required: true, message: "客户类型不能为空", trigger: "change" },
        // ],
        priceSchemeId: [
          { required: true, message: "计价方案不能为空", trigger: "change" },
        ],
        meterStatus: [
          { required: true, message: "表计状态不能为空", trigger: "change" },
        ],
        meterType: [
          { required: true, message: "表计类型不能为空", trigger: "change" },
        ]
      },
      activeNames: ["1"],
      treeList: [],
      meterList: [],
      supplierList: [],
      schemeList: [],
      meterBySupplierList: [],
      customStaus: false,
      areID: null,
    };
  },
  watch: {
    // 根据名称筛选企业树
    areaName(val) {
      this.$refs.tree.filter(val);
    },
    form: {
      deep: true,
      handler(val) {
        this.form = val
      },
    },
    form1: {
      deep: true,
      handler(val) {
        this.form1 = val
      },
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getTreeList();
    this.getPricingPlanList();
  },
  methods: {
    // 查询计价方案
    getPricingPlanList() {
      planSelectList().then((res) => {
        this.schemeList = res
      })
    },
    // 表计类型
    getmeterList() {
      this.loading = true;
      getGasmeterList().then((response) => {
        this.meterList = response.data;
        this.loading = false;
      });
    },
    // 供应商选择后获取表计类型
    getMeterListBySupplier() {
      getGasmeterList({ manufacturerId: this.form1.meterManufacturer }).then((res) => {
        this.meterBySupplierList = res.data
      })
    },
    /** 查询供应商列表 */
    getsupplierList() {
      this.loading = true;
      getSupplierList().then((response) => {
        this.supplierList = response.data;

      });
    },
    /** 获取新增小区 **/
    getTreeList() {
      let param = {
        id: null
      }
      if (!this.customStaus) {
        param.id = this.areID
      } else {
        param.id = null
      }
      areaList(param).then((response) => {
        this.treeList = response.rows;
      });
    },
    /** 查询企业下拉树结构 */
    getDeptTree() {
      areaTreeList().then((response) => {
        this.areaOptions = response.data;
      });
    },
    // 树查询
    treeBlur(event) {
      console.log(event)
    },


    getList() {
      this.loading = true;
      customeList(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 筛选节点
    filterNode(value, data) {
      console.log(data)
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaId = data.id;
      if (data.type === '2') {
        this.customStaus = true
      } else {
        this.customStaus = false
      }
      this.handleQuery();

      this.areID = data.id
      this.getTreeList()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表计取消按钮
    cancel2() {
      this.bjOPen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.areaId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.idName = selection.map((item) => item.customerName);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.getNumber()
      this.title = "添加客户档案";
      if (this.customStaus) {
        this.form.areaId = this.areID
      } else {
        this.form.areaId = null
      }
    },
    // 刷新客户编码
    refreshNumber() {
      this.getNumber()
    },
    // 获取客户编码
    getNumber() {
      generateNumber('21').then((res) => {
        // this.form.customerCode = res.data
        this.$set(this.form, 'customerCode', res.data)
      })
    },
    //  // 刷新表计编码
    //  refreshNumber1(){
    //   this.getNumber1()
    // },
    //  // 获取表计编码
    //  getNumber1(){
    //   generateNumber('22').then((res)=>{
    //     // this.form.customerCode = res.data
    //     this.$set(this.form1,'meterCode',res.data)
    //   })
    // },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.id;
      customeInfo(userId).then((response) => {
        this.form = response.data;
        this.$set(this.form, "postIds", response.postIds);
        this.$set(this.form, "roleIds", response.roleIds);
        this.open = true;
        this.title = "修改客户档案";
        this.form.password = "";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateArea(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustome(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //表计提交按钮
    submitForm2() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          //this.form1.initialGas = this.form1.initialGas ? Number(this.form1.initialGas.replace(/^(0+)|[^\d]+/g, '')) : ""
          //this.form1.meterManufacturer = this.form1.meterManufacturer.toString()
          this.form1.meterType = Number(this.form1.meterType)
          addMeterarchives(this.form1).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.bjOPen = false;
            this.getList();
          });

        }
      })

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let userIds = ''
      let userName = ''
      if (row == '批量') {
        userIds = this.ids.join(',')
        userName = this.idName.join(',')
      } else {
        userIds = row.id
        userName = row.customerName
      }
      this.$modal
        .confirm('是否确认删除客户"' + userName + '"的数据？')
        .then(function () {
          return delCustome(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handlAaddBj(row) {
      console.log(row)
      this.bjOPen = true;
      this.title2 = "添加表计";
      // this.getNumber1()
      // this.getmeterList()
      this.getsupplierList()
      this.getPricingPlanList()
      this.form1 = {
        customerName: row.customerName,
        customerId: row.id,
        meterType: row.meterType,
        isInstallDevice: 'N',
        isGprs: 'N',
        intakeDirection: "L",
        // customerCode:row.customerCode,
        // customerType:row.customerType,
        // idNumber:row.idNumber,
        // telephone:row.telephone,
        // customerAddress:row.customerAddress,
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/customerarchives/export",
        {
          ...this.queryParams,
        },
        `客户档案_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "客户导入";
      this.upload.open = true;
    },
    handleImportOld() {
      this.uploadold.title = "旧客户数据导入";
      this.uploadold.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "system/customerarchives/importTemplate",
        {},
        `客户档案模板_${new Date().getTime()}.xlsx`
      );
    },
    importTemplateOld() {
      this.download(
        "system/customerarchives/importTemplateOld",
        {},
        `旧客户档案模板_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    handleFileUploadProgressOld(event, file, fileList) {
      this.uploadold.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    handleFileSuccessOld(response, file, fileList) {
      this.uploadold.open = false;
      this.uploadold.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
        response.msg +
        "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    submitFileFormOld() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style lang="scss">
.head-containers {
  width: 200px;
  overflow: auto;

  .el-tree {
    min-width: 100%;
    display: inline-block !important;
  }
}

.el-form-item--medium .el-form-item__content {
  line-height: 34px !important;
}

.el-form-item--medium .el-form-item__label {
  line-height: 37px !important;
}
</style>
